//
//  JHSSicknessViewController.m
//  病例管理
//
//  Created by chenWei on 2017/3/24.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "JHSSicknessViewController.h"
#import "JHSSickDetailCell.h"
#import "JHSSicknessManageController.h"


//全局变量：病例添加行数
NSInteger row = 0;


@interface JHSSicknessViewController ()<UITableViewDataSource,UITableViewDelegate,JHSSicknessManageControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tabView;

//记录疾病管理控制器传过来的模型数据
@property (nonatomic, strong) JHSSicknessModel *sicknessModel;


@property (nonatomic, strong) JHSSicknessManageController *managerVC;

//记录下标
@property (nonatomic, assign) NSInteger index;



@end

//重用标识
static NSString *detailCellID = @"detailCellID";


@implementation JHSSicknessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];

    
    //实例化模型数组
    _sickManagerModelArrM = [NSMutableArray array];
    

    
}


//创建及添加子控件
- (void)setupUI
{
    
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    
    //视图在启动的时候让其隐藏
    self.tabView.hidden = YES;
    
    self.tabView.rowHeight = 200;
    
    
    //加载 xib
    UINib *nib = [UINib nibWithNibName:@"JHSSickDetailCell" bundle:nil];
    
    [self.tabView registerNib:nib forCellReuseIdentifier:detailCellID];
    
    
    //通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeModelCell) name:@"delete" object:nil];
    
    
}

//设置疾病管理控制器的代理
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    JHSSicknessManageController *sickManagerVC = segue.destinationViewController;
    
    sickManagerVC.delegate = self;
    
    _managerVC = sickManagerVC;
    
    
    
    //实例化模型
//    _sicknessModel = [[JHSSicknessModel alloc]init];
    
}



//MARK:1- 实现疾病管理控制器的代理方法 把模型传过来
- (void)sicknessManageController:(JHSSicknessManageController *)sickManagerVC withSicknessModelArrM:(NSMutableArray *)sicknManagerModelArrM
{
    [_sickManagerModelArrM  addObjectsFromArray:sicknManagerModelArrM];
    
    [self.tabView reloadData];
}


//MARK:2- 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //记录下标
    _index = indexPath.row;
    
    //动画取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //push到疾病管理控制器
#warning mark - 推送控制器要看是不是同一个对象，使用重新创建的控制器有 bug
    [self.navigationController pushViewController:_managerVC animated:YES];
    
    
}


//界面将要显示的时候来调用的方法
- (void)viewDidAppear:(BOOL)animated
{
    //给 tableView 添加尾部视图
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor lightGrayColor];
    self.tabView.tableFooterView = view;
    
}


// MARK:3- 通知调用的方法
- (void)removeModelCell
{
    [_sickManagerModelArrM removeObjectAtIndex:_index];
    
    
    //调用 cell 删除的方法
    if (_sickManagerModelArrM.count < 1) {
        
        self.tabView.hidden = YES;
    }
    
    [self.tabView reloadData];
    
}

//移除通知
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
{
    [[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
}


#pragma mark - 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_sickManagerModelArrM != nil) {
        
        self.tabView.hidden = NO;
    }
    
    return _sickManagerModelArrM.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JHSSickDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:detailCellID forIndexPath:indexPath];
    
    //给模型赋值
    cell.sicknessModel = _sickManagerModelArrM[indexPath.row];
    
    
    return cell;
    
}









@end
